#include <REGX52.H>

sbit I2C_SCL=P2^1;
sbit I2C_SDA=P2^0;



//I2C起始条件
void I2C_Start (void)
{
	I2C_SCL=1;
	I2C_SDA=1;
	I2C_SDA=0;
	I2C_SCL=0;
}

//I2C终止条件
void I2C_Stop(void)
{
	I2C_SDA=0;
	I2C_SCL=1;
	I2C_SDA=1;
}

//发送一个字节
//Byte要发送的字节
void I2C_SendByte(unsigned char Byte)
{
	unsigned char i = 0;
	for(i = 0;i < 8; i++)
	{
		I2C_SDA=(Byte&(0x80>>i));
		I2C_SCL=1;
		I2C_SCL=0;
	}
}

//接收一个字节
//返回值接收的一个字节数据
unsigned char I2C_ReceiveByte (void)
{
	unsigned char Byte = 0;
	unsigned char i = 0;
	I2C_SDA=1;
	for(i = 0; i < 8; i++)
	{
		I2C_SCL=1;
		if(I2C_SDA==1)
		{
			Byte|=(0x80>>i);
		}
		I2C_SCL=0;
	}
	return Byte;
}


//发送应答
//AckBit//0为发送应答//1为未应答
void I2C_SendAck(unsigned char AckBit)
{
//	I2C_SCl=0;
//	I2C_SDA=1;
//	I2C_SCl=1;
	I2C_SDA=AckBit;
	I2C_SCL=1;
	I2C_SCL=0;
}


//接收应答
//返回值
//接收到的应答位//0为接收应答//1为未接收应答
unsigned char I2C_ReceiveAck(void)
{
	unsigned char AckBit = 0;
	I2C_SDA=1;
	I2C_SCL=1;
	AckBit=I2C_SDA;
	I2C_SCL=0;
	return AckBit;
}